package com.strelitzia.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.strelitzia.entity.PhotoEntity;
import com.strelitzia.exception.StrelitziaException;
import com.strelitzia.service.PhotoService;
import com.strelitzia.vo.PhotoItem;
import com.strelitzia.vo.ResultVo;
import com.strelitzia.vo.UploadFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @create 2024/1/12 19:32
 */
@RequestMapping("/photo")
@RestController
public class PhotoController {
    @Autowired
    PhotoService photoService;

    @PostMapping("/upload")
    public ResultVo upload(UploadFrom uploadFrom) {
        ResultVo resultVo = new ResultVo();
        try {
            photoService.upload(uploadFrom);
            resultVo.setMsg("上传成功");
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("上传失败");
        }
        return resultVo;
    }

    @GetMapping("/list/{albumId}")
    public ResultVo list(@PathVariable("albumId") Integer albumId) {
        ResultVo resultVo = new ResultVo();
        try {


            resultVo.setData(photoService.getList(albumId));

        } catch (Exception e) {
            resultVo.setCode(1);
            if (e instanceof StrelitziaException) {
                resultVo.setMsg(e.getMessage());
            } else {
                resultVo.setMsg("服务器错误");
            }
        }
        return resultVo;
    }

    @GetMapping("/count/{uid}")
    public Integer count(@PathVariable("uid") Integer uid) {
        return photoService.count(new QueryWrapper<PhotoEntity>().eq("uid", uid));
    }

    @GetMapping("/list")
    public ResultVo list() {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(photoService.getAllList());
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("服务器错误");
        }
        return resultVo;
    }

    @GetMapping("/list/del")
    public ResultVo delList() {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(photoService.getDelList());
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("服务器错误");
        }
        return resultVo;
    }

    @GetMapping("/del/{id}")
    public ResultVo del(@PathVariable("id") Integer id) {
        ResultVo resultVo = new ResultVo();
        try {
            photoService.del(id);
            resultVo.setMsg("已将该照片移入回收站");
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("删除失败");
        }
        return resultVo;
    }

    @GetMapping("/restore/{id}")
    public ResultVo restore(@PathVariable("id") Integer id) {
        ResultVo resultVo = new ResultVo();
        try {
            photoService.restore(id);
            resultVo.setMsg("还原图片成功");
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("服务器错误");
        }
        return resultVo;
    }

    @GetMapping("/clear")
    public ResultVo clear() {
        ResultVo resultVo = new ResultVo();
        try {
            photoService.clear();
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("服务器错误");
        }
        return resultVo;
    }
}
